using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TXLooker.WorkItemExtention.UI.QueryManager
{
    internal class DataGridViewQueryCell : DataGridViewTextBoxCell
    {
        // Fields
        private int _heightOfRowBeforeEditMode;
        private const string DEFAULT_STRING = "";

        // Methods
        public override void DetachEditingControl()
        {
            if (this._heightOfRowBeforeEditMode > 0)
            {
                base.OwningRow.Height = this._heightOfRowBeforeEditMode;
            }
            base.DetachEditingControl();
        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            if (value == null)
            {
                return "";
            }
            return value;
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
            ComboBoxEdit editingControl = base.DataGridView.EditingControl as ComboBoxEdit;
            this._heightOfRowBeforeEditMode = base.OwningRow.Height;
            base.OwningRow.Height = editingControl.Height;
            Query q = base.Value as Query;
            if (q == null)
            {
                q = new Query();
            }
            editingControl.SetupControls(q);
        }

        // Properties
        public override object DefaultNewRowValue
        {
            get
            {
                return "";
            }
        }

        public override Type EditType
        {
            get
            {
                return typeof(ComboBoxEdit);
            }
        }

        public override Type FormattedValueType
        {
            get
            {
                return typeof(Query);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(Query);
            }
        }
    }

}